﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Management_MedicalDirector_Nhacungcap : System.Web.UI.Page
{
    public static string idTotal = "";
    public static Color defautColor;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            defautColor = txtNameNCC.BackColor;
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void nhacungcap_click(object sender, EventArgs e)
    {
        string id = (sender as LinkButton).CommandArgument;
        idTotal = id;
        lbTilte2.Text = id;
        SqlConnection con = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        con.Open();
        string sql = "SELECT * FROM tbSupplier WHERE ID=" + id;
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            txtNameNCC.Text = reader.GetString(1);
            txtAddressNCC.Text = reader.GetString(2);
            txtTelNCC.Text = reader.GetString(3);
            lbTilte2.Text = "Thông tin nhà cung cấp";
            btOk.Text = "Edit";
        }
        txtNameNCC.Enabled = false;
        txtAddressNCC.Enabled = false;
        txtTelNCC.Enabled = false;
        reader.Close();
        con.Close();
        pnInfo.Visible = true;
    }
    protected void edit_click(object sender, EventArgs e)
    {
        if (btOk.Text.Equals("Edit"))
        {
            txtNameNCC.Enabled = true;
            txtAddressNCC.Enabled = true;
            txtTelNCC.Enabled = true;
            txtNameNCC.BackColor = Color.White;
            txtAddressNCC.BackColor = Color.White;
            txtTelNCC.BackColor = Color.White;
            txtNameNCC.Enabled = true;
            txtAddressNCC.Enabled = true;
            txtTelNCC.Enabled = true;
            btOk.Text = "OK";
            txtNameNCC.Focus();
        }
        else
            if (btOk.Text.Equals("OK"))
            {
                SqlConnection con = new SqlConnection(GlobalConstant.CONNECTION_STRING);
                con.Open();
                string sql = "UPDATE tbSupplier SET Name=@Name, Address=@Address, Tel=@Tel" + " WHERE Id=@Id";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@Name", txtNameNCC.Text);
                cmd.Parameters.AddWithValue("@Address", txtAddressNCC.Text);
                cmd.Parameters.AddWithValue("@Tel", txtTelNCC.Text);
                cmd.Parameters.AddWithValue("@ID", idTotal);
                cmd.ExecuteNonQuery();
                txtNameNCC.BackColor = defautColor;
                txtAddressNCC.BackColor = defautColor;
                txtTelNCC.BackColor = defautColor;
                txtNameNCC.Enabled = false;
                txtAddressNCC.Enabled = false;
                txtTelNCC.Enabled = false;
                GridView1.DataBind();
                btOk.Text = "Edit";
                con.Close();
            }
            else
            {
                SqlConnection con = new SqlConnection(GlobalConstant.CONNECTION_STRING);
                con.Open();
                string sql = "INSERT INTO tbSupplier (Name, Address, Tel) VALUES (@Name, @Address, @Tel)";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@Name", txtNameNCC.Text);
                cmd.Parameters.AddWithValue("@Address", txtAddressNCC.Text);
                cmd.Parameters.AddWithValue("@Tel", txtTelNCC.Text);
                cmd.ExecuteNonQuery();
                txtNameNCC.BackColor = defautColor;
                txtAddressNCC.BackColor = defautColor;
                txtTelNCC.BackColor = defautColor;
                txtNameNCC.Enabled = false;
                txtAddressNCC.Enabled = false;
                txtTelNCC.Enabled = false;
                GridView1.DataBind();
                btOk.Text = "Edit";
                con.Close();
            }
    }
    protected void addnew_click(object sender, EventArgs e)
    {
        txtNameNCC.Text = "";
        txtAddressNCC.Text = "";
        txtTelNCC.Text = "";
        pnInfo.Visible = true;
        txtNameNCC.BackColor = Color.White;
        txtAddressNCC.BackColor = Color.White;
        txtTelNCC.BackColor = Color.White;
        txtNameNCC.Enabled = true;
        txtNameNCC.Focus();
        txtAddressNCC.Enabled = true;
        txtTelNCC.Enabled = true;
        btOk.Text = "Add";
    }
}